﻿using System;
using System.Data;
using System.Collections.Generic;

using System.Text;
using System.Linq;
using WinnerCMS.Model;
using WinnerCMS.Common;
using System.Data.Entity;
namespace WinnerCMS.Data
{
    /// <summary>
    /// DA_Friend_Link 的摘要说明。
    /// </summary>
    public class FriendLinkDal  
    {
        /// <summary>
        /// 添加链接
        /// </summary>
        /// <param name="model"></param>
        public void Add(FriendLink model)
        {
            using (Entities db = new Entities())
            {
                db.FriendLinks.Add(model);
                db.SaveChangesAsync().Wait();

                int maxOrderId = db.FriendLinks.Max(x => x.OrderId);
                db.Entry(model).State = EntityState.Modified;
                model.OrderId = maxOrderId + 1;
                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 修改链接
        /// </summary>
        /// <param name="model"></param>
        public void Edit(FriendLink model)
        {
            using (Entities db = new Entities())
            {
                db.Entry(model).State = EntityState.Modified;

                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 删除链接
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            using (Entities db = new Entities())
            {
                var q = db.FriendLinks.FirstOrDefault(x => x.Id == id);
                if (q != null)
                {
                    db.Database.ExecuteSqlCommand("update CMS_FriendLinks set OrderId=OrderId-1 where OrderId>" + q.OrderId);

                    db.FriendLinks.Remove(q);
                    db.SaveChangesAsync().Wait();
                }
            }
        }
        /// <summary>
        /// 加载链接
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FriendLink GetInfo(int id)
        {
            using (Entities db = new Entities())
            {
                return db.FriendLinks.FirstOrDefault(x => x.Id == id);
            }
        }
        /// <summary>
        /// 绑定友情链接--后台使用
        /// </summary>
        /// <returns></returns>
        public IList<FriendLink> BindList()
        {
            using (Entities db = new Entities())
            {
                return db.FriendLinks.OrderBy(x => x.OrderId).ToArray();
            }
        }

        /// <summary>
        /// 绑定友情链接--前台使用
        /// </summary>
        /// <param name="type"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        public IList<FriendLink> BindList(string type, bool pass)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.FriendLinks select x;
                if (type == "logo")
                {
                    q = q.Where(x => x.Logo != "");
                }
                else
                {
                    q = q.Where(x => x.Logo == "");
                }
                return q.OrderBy(x => x.OrderId).ToArray();
            }
        }


        /// <summary>
        /// 链接排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        public void UpDown(string id, string type)
        {
            //得到本链接的信息
            object objOrderid = SQLBaseV2.ExecuteScalar("select OrderId from CMS_FriendLinks where Id=" + id);
            if (type.ToLower() == "up")
            {
                //得到上面的链接的信息
                DataRow dr = SQLBaseV2.ReturnDataRow("select Id,OrderId  from CMS_FriendLinks where OrderId=(select Max(OrderId) from CMS_FriendLinks where OrderId<" + objOrderid + ")");
                if (dr != null)
                {
                    //原链接赋予上面的那个链接的排序Id
                    SQLBaseV2.ExecuteNonQuery("update CMS_FriendLinks set OrderId=" + dr["OrderId"] + " where Id=" + id);
                    //交换OrderId
                    SQLBaseV2.ExecuteNonQuery("update CMS_FriendLinks set OrderId=" + objOrderid + " where Id=" + dr["Id"]);
                }
            }
            else
            {
                //得到下面面的链接的信息
                DataRow dr = SQLBaseV2.ReturnDataRow("select Id,OrderId  from CMS_FriendLinks where OrderId=(select top 1 OrderId from CMS_FriendLinks where OrderId>" + objOrderid + " order by OrderId asc)");
                if (dr != null)
                {
                    //原链接赋予下面的那个链接的排序Id
                    SQLBaseV2.ExecuteNonQuery("update CMS_FriendLinks set OrderId=" + dr["OrderId"] + " where Id=" + id);
                    //交换OrderId
                    SQLBaseV2.ExecuteNonQuery("update CMS_FriendLinks set OrderId=" + objOrderid + " where Id=" + dr["Id"]);
                }
            }

        }
    }
}